package bit.springblogdemo.controller;


import bit.springblogdemo.pojo.request.AddBlogRequest;
import bit.springblogdemo.pojo.request.UpdateBlogRequest;
import bit.springblogdemo.pojo.response.BlogInfoResponse;

import bit.springblogdemo.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Resource(name="blogServiceImpl")
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfoResponse>getList(){
        log.info("获取博客链表..");
        List<BlogInfoResponse>blogInfos=blogService.getList();

        return blogInfos;
    }


    @RequestMapping("/getBlogDetail")
    public BlogInfoResponse getBlogDetail(@NotNull Integer blogId){
        log.info("获取博客详情,blogId:{}",blogId);
        return blogService.getBlogDetail(blogId);
    }

    /**
     * 添加博客
     */
    @RequestMapping("/addBlog")
    public Boolean addBlog(@RequestBody @Validated AddBlogRequest addBlogRequest){
        log.info("添加博客,userId:{},title:{]",addBlogRequest.getUserId(),addBlogRequest.getTitle());

        return blogService.addBlog(addBlogRequest);
    }


    @RequestMapping("/update")
    //必须加上@VALIDATED注解，否则notnull检验不生效
    public Boolean updataBlog(@RequestBody @Validated UpdateBlogRequest updateBlogRequest){
        log.info("修改博客信息,e:{}",updateBlogRequest);

        return blogService.updateBlog(updateBlogRequest);
    }

    @RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId){
        log.info("删除博客信息,e:{}",blogId);

        return blogService.deleteBlog(blogId);//数据库的查询字段是根据id而不是blogId
        // 也就是根据mapper中的值判断，即AddBlogRequest中定义的字段
    }
}
